﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IterationLife.Domains.Sports.Enums;
using IterationLife.Domains.Sports.Models;
using Util;

namespace IterationLife.Domains.Sports.Dtos.ZeppSports
{
    public class ZeppUserInfo
    {
        public string id { get; set; }
        public string name { get; set; }
        public int? gender { get; set; }
        public ZeppFile avatar { get; set; }
        public decimal? weight { get; set; }
        public decimal? height { get; set; }
        public decimal? age { get; set; }
        public string mobilePhoneNumber { get; set; }
        public string username { get; set; }
        public string sessionToken { get; set; }
    }

    public static class ZeppUserInfoExtension
    {
        public static SportUser ToSportUser(this ZeppUserInfo userInfo)
        {
            var result = new SportUser(Guid.NewGuid());
            SetEntity(result, userInfo);
            return result;
        }

        public static void SetEntity(SportUser sportUser, ZeppUserInfo userInfo)
        {
            sportUser.EquipmentType = EquipmentType.Zepp;
            sportUser.PartnerUserId = userInfo.id;
            sportUser.Mobile = userInfo.mobilePhoneNumber;
            sportUser.Token = userInfo.sessionToken;
            sportUser.Photo = userInfo.avatar?.url;
        }

        public static SportAthlete ToAthlete(this ZeppUserInfo userInfo, Guid id)
        {
            var result = new SportAthlete(id);
            SetAthleteEntity(result, userInfo);
            return result;
        }

        public static void SetAthleteEntity(SportAthlete sportAthlete, ZeppUserInfo userInfo)
        {
            sportAthlete.PartnerAthleteId = userInfo.id;
            sportAthlete.Weight = userInfo.weight;
            sportAthlete.Stature = userInfo.height;
            sportAthlete.Birthday = DateTime.Now.AddYears(-(int)userInfo.age.SafeValue());
            sportAthlete.Sex = userInfo.gender.SafeValue();
        }
    }
}